package org.jeecg.modules.oa.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.oa.entity.OaComment;
import org.jeecg.modules.oa.entity.OaCommentRecord;
import org.jeecg.modules.oa.mapper.OaCommentMapper;
import org.jeecg.modules.oa.mapper.OaCommentRecordMapper;
import org.jeecg.modules.oa.service.IOaCommentRecordService;
import org.jeecg.modules.oa.service.IOaCommentService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 评论表
 * @Author: jeecg-boot
 * @Date:   2019-09-12
 * @Version: V1.0
 */
@Service
public class OaCommentServiceImpl extends ServiceImpl<OaCommentMapper, OaComment> implements IOaCommentService {
    @Autowired
    private OaCommentMapper oaCommentMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IOaCommentRecordService commentRecordService;
    @Override
    public Result<List<OaComment>> getByTopicIdAndParentIdIsNull(String topicId) {
        Result<List<OaComment>> result = new Result<>();
        List<OaComment> oaComments = oaCommentMapper.getByTopicId( topicId );
        if(CollectionUtil.isNotEmpty(oaComments)){
            for(OaComment oc:oaComments){
                SysUser user = sysUserService.getById(oc.getCreateBy());
                oc.setSysUser(user);
                //评论的评论
                List<OaComment> childList = oaCommentMapper.getByParentId(oc.getId());
                if(CollectionUtil.isNotEmpty(childList)){
                    oc.setChildList(childList);
                }
            }
            result.setSuccess(true);
            result.setResult(oaComments);
        }
        return result;
    }
    @Override
    public Result<List<OaComment>> getByParentId(String parentId) {
        Result<List<OaComment>> result = new Result<List<OaComment>>();
        List<OaComment> oaComments=oaCommentMapper.getByTopicId( parentId );
        if(CollectionUtil.isNotEmpty(oaComments)){
            result.setSuccess(true);
            result.setResult(oaComments);
        }
        return result;
    }

    @Override
    public List<OaComment> findAllMsgById(String userId, String topicId) {
        OaCommentRecord recordMsg = commentRecordService.findReplyIdByTopicId(topicId, userId);
        if (recordMsg == null) {
            OaCommentRecord record = new OaCommentRecord();
            record.setTopicId(topicId);
            record.setUserId(userId);
            commentRecordService.save(record);
        } else {
            commentRecordService.updateById(recordMsg);
        }
        List<OaComment> oaComments = oaCommentMapper.findAllByTopicId(topicId);
        if(CollectionUtil.isNotEmpty(oaComments)){
            for(OaComment oc:oaComments){
                SysUser user = sysUserService.getById(oc.getCreateBy());
                oc.setSysUser(user);
            }
        }
        return oaComments;
    }

    @Override
    public List<OaComment> getByTopicId(String topicId) {
        return oaCommentMapper.getByTopicId(topicId);
    }


}
